package com.lsy.weizhi2.utils;

import android.content.Context;
import android.content.res.AssetManager;

import com.google.gson.Gson;
import com.lsy.weizhi2.JsonObject.City;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 嗨哟 on 2016/12/16.
 */
public class TextHelper {
    private static Gson gson;
    private static City addrs;
    public static List<String>provinces=new ArrayList<>();
    public static Map<String,List<String>>cityss=new HashMap<>();
    public static Map<String,List<String>>districts=new HashMap<>();
    public static String[]province;
    public static Map<String,String[]>citys=new HashMap<>();
    public static Map<String,String[]>district=new HashMap<>();
    public static String[]time,date;
    private static String getCity(Context context){
        AssetManager manager=context.getAssets();
        StringBuffer sb=new StringBuffer();
        try {
            InputStream is=manager.open("city.json");
            int len = -1;
            byte[] buf = new byte[2048];//一个中文汉字占两个字节
            while ((len = is.read(buf)) != -1) {
                sb.append(new String(buf, 0, len, "utf-8"));
            }
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String json=sb.toString();
        return json;
    }
    public static void AnalyzeCity(Context context){
        gson=new Gson();
        String json_city= getCity(context);
        addrs=gson.fromJson(json_city,City.class);
        int length_p=addrs.getCitylist().size();
        if(length_p!=0){
            province=new String[length_p];
            List<String> c1=new ArrayList<>();
            for(int i=0;i<length_p;i++){
                String p=addrs.getCitylist().get(i).getP();
                provinces.add(p);
                province[i]=p;
                int length_c=addrs.getCitylist().get(i).getC().size();
                if(length_c!=0){
                    String[]ccc=new String[length_c];
                    List<String>d1=new ArrayList<>();
                    for (int j = 0; j <length_c ; j++) {
                        String c=addrs.getCitylist().get(i).getC().get(j).getN();
                        c1.add(c);
                        ccc[j]=c;
                        int length_d=0;
                        try{
                            length_d=addrs.getCitylist().get(i).getC().get(j).getA().size();
                        }catch (Exception e){
                            continue;
                        }
                        if(length_d>0){
                            String[]ddd=new String[length_d];
                            for (int k = 0; k < length_d; k++) {
                                String d=addrs.getCitylist().get(i).getC().get(j).getA().get(k).getS();
                                d1.add(d);
                                ddd[k]=d;
                            }
                            districts.put(c,d1);
                            district.put(c,ddd);
                        }
                    }
                    cityss.put(p,c1);
                    citys.put(p,ccc);
                }
            }
        }
        time=new String[60];
        for (int i = 0; i < 60; i++) {
            time[i]=(i+1)+"";
        }
        date=new String[]{"天","周","月","年"};
    }
}
